__doc__ =   """ Class with utilites to construct PDB/Structure processing pipleines
            """

from PDBExceptions import PDBConstructionError
from PDBParser import PDBParser
from PDBWriter import PDBWriter
from EntityMisc import gzip_load

# initialize parser/writer
pdb_parser = PDBParser(forgive = 1)
pdb_writer = PDBWriter()

def clean_list(files, errors):
    """ remove filenames which are in the error list
    """
    return [file for file in files if file not in errors]

def parse_(directory, file):
    """ Parse file in a directory and return a structure.
    """
    pdb_parser.set_file(directory + file)
    return pdb_parser.get_structure(file[0:4])

def load_(directory, file_):
    """ Load pickle file in a direcotry and returns a structure
    """
    file_ = directory + file_[0:4]
    structure = gzip_load(file_)
    return structure

def structure_parser(directory, file_list):
    """ Generator of structures from a directory.
    """
    for file in file_list:
        try:
           structure = parse_(directory, file)
        except (IOError, PDBConstructionError):
           structure = None
        yield structure

def structure_loader(directory, file_list):
    for file in file_list:
        try:
           structure = load_(directory, file[0:4])
        except IOError:
           structure = None
        yield structure